package com.ruoyi.khpx.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class DeepSeekUitls {
    public static final String API_URL = "https://api.deepseek.com/chat/completions";
    public static final String API_KEY = "sk-2ccd7932a6974071bbace56a375f9858"; //


    public String askDeepSeek(String question) throws IOException, ParseException {
        CloseableHttpClient client = HttpClients.createDefault();

        // 创建 HTTP POST 请求
        HttpPost request = new HttpPost(API_URL);
//        request.setHeader("Content-Type", "application/json");
        request.setHeader("Authorization", "Bearer " + API_KEY);
        request.setHeader("Accept-Language", "zh-CN");

        // 动态构建请求体
        String requestBody = String.format(

                "{\"language\":\"zh-CN\",\"model\": \"deepseek-chat\", \"messages\": [{\"role\": \"system\", \"content\": \"You are an assistant, please answer the user's questions in Chinese.\"},{\"role\": \"user\", \"content\": \"%s\"}], \"stream\": true}",
                question
        );
        // 设置请求实体并指定字符编码
        StringEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON.withCharset("UTF-8"));
        request.setEntity(entity);
        // 发送请求并获取响应
        try (CloseableHttpResponse response = client.execute(request)) {
            // 返回响应内容
//            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));
//            if (jsonObject.get("choices") != null) {
//                JSONArray choices = jsonObject.getJSONArray("choices");
//                if (choices.size() > 0) {
//                    JSONObject choice = choices.getJSONObject(0);
//                    if (choice.get("message") != null) {
//                        JSONObject message = choice.getJSONObject("message");
//                        if (message.get("content") != null) {
//                            return message.getString("content");
//                        }
//                   }
//                }
//            }
            // 读取流式响应
            InputStream inputStream = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            StringBuilder responseBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                responseBuilder.append(line);
            }
            String responseBody = responseBuilder.toString();

            // 解析响应
            JSONObject jsonObject = JSON.parseObject(responseBody);
            if (jsonObject.get("choices") != null) {
                JSONArray choices = jsonObject.getJSONArray("choices");
                if (choices.size() > 0) {
                    JSONObject choice = choices.getJSONObject(0);
                    if (choice.get("message") != null) {
                        JSONObject message = choice.getJSONObject("message");
                        if (message.get("content") != null) {
                            return message.getString("content");
                        }
                    }
                }
            }
            return EntityUtils.toString(response.getEntity());
        }
    }
}
